约 2704 字

约 14 分钟

#HTML 的 <col> 标签

HTML 的 <col> 元素在其父 <colgroup> 元素所代表的列组中定义一列或多列。

使用 <col> 元素会导致父元素 <colgroup> 的 span 属性失效。

#属性

  • span: 指定 <col> 元素跨越的连续列数。该值必须是大于 0 的正整数。如果不存在,其默认值为 1。

#示例

  1. 基本示例:

    <table>
    <colgroup>
        <col style="background-color:yellow"> <!-- 第一列 -->
        <col style="background-color:lightblue"> <!-- 第二列 -->
        <col style="background-color:lightgreen"> <!-- 第三列 -->
    </colgroup>
    <tr>
        <th>姓名</th>
        <th>年龄</th>
        <th>城市</th>
    </tr>
    <tr>
        <td>张三</td>
        <td>28</td>
        <td>北京</td>
    </tr>
    <tr>
        <td>李四</td>
        <td>32</td>
        <td>上海</td>
    </tr>
    </table>
    

    <col>

  2. span 属性跨列示例:

    <table>
    <colgroup>
        <col style="background-color:lightblue"> <!-- 第一列 -->
        <col span="2" style="background-color:lightgreen"> <!-- 两列设为同一种样式 -->
    </colgroup>
    <tr>
        <th>产品</th>
        <th>价格</th>
        <th>库存</th>
    </tr>
    <tr>
        <td>手机</td>
        <td>$599</td>
        <td>25</td>
    </tr>
    <tr>
        <td>笔记本</td>
        <td>$999</td>
        <td>12</td>
    </tr>
    </table>
    

    <col>

#推荐阅读

更新: 2026/7/2

作者: planc

创建: 2025/6/9